package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Curriculum;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.ICurriculumService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("curriculum")
public class CurriculumController {
    @Autowired
    private ICurriculumService curriculumService;

    /*创建一个方法来确定*/
    @RequestMapping("index")
    public String index(){
        //查找到并共享
        return "curriculum/list";
    }
    @RequiresPermissions("curriculum:list")
    @PermissionName("课程列表")
    @ResponseBody
    @RequestMapping("list")
    public PageResult list(Model model, @ModelAttribute("qo")QueryForObject qo){
        return curriculumService.query(qo);
    }

    /**
     *
     * @param curriculum 保存课程系列
     * @return 返回json
     */
    @RequiresPermissions("curriculum:save")
    @PermissionName("课程保存")
    @ResponseBody
    @RequestMapping("save")
    public JsonResult save(Curriculum curriculum){
        JsonResult result = new JsonResult();
        try {
            curriculumService.insert(curriculum);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;
    }
    @RequiresPermissions("curriculum:update")
    @PermissionName("课程更新")
    @ResponseBody
    @RequestMapping("update")
    public JsonResult update(Curriculum curriculum){
        JsonResult result = new JsonResult();
        try {
            curriculumService.updateByPrimaryKey(curriculum);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新失败");
        }
        return result;
    }
    //删除课程系列
    @RequiresPermissions("curriculum:delete")
    @PermissionName("课程删除")
    @ResponseBody
    @RequestMapping("delete")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            curriculumService.deleteByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("删除失败");
        }
        return result;
    }
    //课程表的回显 用在课程表里面
    @ResponseBody
    @RequestMapping("selectAll")
    public List<Curriculum> selectAll(){
        return curriculumService.selectAll();

    }


}
